﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Collections;
using Google.API.Translate;

namespace Dictionary.PresentationLayer
{
    /// <summary>
    /// Interaction logic for Translator.xaml
    /// </summary>
    public partial class Translator : Window
    {
        public Translator()
        {
            InitializeComponent();
        }

#region Function
        //ham goi effect cua cac nut min, max, close
        private void ControlButton(System.Windows.Controls.Image i, string s)
        {
            i.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5);
            Storyboard stb = new Storyboard();
            stb = (Storyboard)Resources[s];
            i.BeginStoryboard(stb);
        }

        private void ScaleControl(TextBox ctr)
        {
            Storyboard stb = new Storyboard();
            stb = (Storyboard)Resources["ScaleControl"];
            ctr.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5);
            ctr.BeginStoryboard(stb);
        }
#endregion

        private void imgClose_MouseEnter(object sender, MouseEventArgs e)
        {
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = new Uri("../Image/Close_2.png", UriKind.Relative);
            bi.EndInit();
            ImageSource img = bi;
            imgClose.Source = img;
            ControlButton(imgClose, "ControlButton_Down");
        }

        private void imgClose_MouseLeave(object sender, MouseEventArgs e)
        {
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = new Uri("../Image/Close.png", UriKind.Relative);
            bi.EndInit();
            ImageSource img = bi;
            imgClose.Source = img;
            ControlButton(imgClose, "ControlButton_Up");
        }

        private void imgClose_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void lblTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void txtSrc_MouseEnter(object sender, MouseEventArgs e)
        {
            ScaleControl(txtSrc);
        }

        private void txtDes_MouseEnter(object sender, MouseEventArgs e)
        {
            ScaleControl(txtDes);
        }

        private void bttTranslate_Click(object sender, RoutedEventArgs e)
        {
            if (txtSrc.Text != "")
            {
                try
                {
                    string src = "", des = "";
                    int s = cbxSrc.SelectedIndex;
                    int d = cbxDes.SelectedIndex;
                    ICollection<Google.API.Translate.Language> lang = Google.API.Translate.Language.TranslatableCollection;
                    int i = 0;
                    foreach (string str in lang)
                    {
                        if (i == s)
                            src = str;
                        if (i == d)
                            des = str;
                        i++;
                    }

                    TranslateClient client = new TranslateClient(null);
                    txtDes.Text = client.Translate(txtSrc.Text, src, des);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Không thể kết nối với Internet !", "Dịch Đoạn Văn Bằng Internet");
                }
            }
        }

        private void bttSwap_Click(object sender, RoutedEventArgs e)
        {
            int i = cbxSrc.SelectedIndex;
            cbxSrc.SelectedIndex = cbxDes.SelectedIndex;
            cbxDes.SelectedIndex = i;
        }
    }
}
